package com.eliza.save.activity

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.eliza.library.tools.InfoTools
import com.eliza.save.R


/**
 * 大多数机器其存储位置在/data/data/<包名>/shared_prefs目录下。
 * 少部分机器会将其存储在/dbdata/databases/<包名>/shared_prefs目录下,
 * */
class SharedPreferencesActivity : AppCompatActivity() {
    private var context = this
    private var File_Name = "UserData"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences)

        var tv = findViewById<TextView>(R.id.SharedPreferences_textView);
        var accout = findViewById<EditText>(R.id.SharedPreferences_editText_accout);
        var pwd = findViewById<EditText>(R.id.SharedPreferences_editText_pwd);

        findViewById<Button>(R.id.SharedPreferences_write_btn).setOnClickListener {
            /**
             * 数据保存位置
             * /data/data/com.eliza.save/shared_prefs/File_Name.xml
             * */
            val shp = getSharedPreferences(File_Name, MODE_PRIVATE)
            val editor = shp.edit()
            editor.putString("accout", accout.text.toString().trim())
            editor.putString("pwd", pwd.text.toString().trim())
            if (editor.commit()) {
                InfoTools.ToastTools(context, "保存成功")
            }
        }

        findViewById<Button>(R.id.SharedPreferences_read_btn).setOnClickListener {
            val shp = getSharedPreferences(File_Name, MODE_PRIVATE)
            tv.text =
                "accout:${shp.getString("accout", " 无 ")}\n pwd:${shp.getString("pwd", " 无 ")}"
        }
    }

}